<!DOCTYPE html>
<html>
	<head>
	    <title>各种查询参数示例</title>
	    <meta charset="utf-8">
		<meta name="viewport" content="width=device-width,initial-scale=1,minimum-scale=1,maximum-scale=1,user-scalable=no" />
		<!-- 所有的 css & js 资源 -->
		<link rel="stylesheet" href="https://unpkg.com/element-ui@2.13.0/lib/theme-chalk/index.css">
		<link rel="stylesheet" href="../../static/sa.css"> 
		<script src="https://unpkg.com/vue@2.6.10/dist/vue.min.js"></script>
		<script src="https://unpkg.com/element-ui@2.13.0/lib/index.js"></script>
		<script src="https://unpkg.com/jquery@3.4.1/dist/jquery.min.js"></script>
		<script src="https://www.layuicdn.com/layer-v3.1.1/layer.js"></script>
		<script src="../../static/sa.js"></script>
        <style>
			.td-img{width: 180px; height: 90px; cursor: pointer; vertical-align: middle;}
			.c-panel-add .td-img{width: 200px;}
			.c-panel-add .el-form .el-input{width: 200px;}
        </style>
	</head>
	<body>
		<!-- 防止margin向下击穿 -->
		<div style="margin-top: -1em;"><br></div>
		<div class="vue-box" style="display: none;" :style="'display: block;'">
			<div class="c-panel">
				<!-- 参数栏 -->
				<div class="c-title">检索参数</div>
				<el-form size="mini" @submit.native.prevent >
					<div class="c-item">
						<label class="c-label">标题：</label>
						<el-input size="mini" v-model="p.title"></el-input>
					</div>
					<div class="c-item" style="min-width: 0px;">
						<el-button size="mini" type="primary" icon="el-icon-search" @click="p.pageNo = 1; f5()">查询</el-button>
					</div>
					<br>
					<div class="c-item">
						<label class="c-label">排序：</label>
						<el-radio-group size="mini" v-model="p.sort_type">
							<el-radio :label="0">排序值</el-radio>
							<el-radio :label="1">创建时间</el-radio>
							<el-radio :label="2">点击量</el-radio>
						</el-radio-group>
					</div>
				</el-form>
				<!-- 数据栏 -->
				<div class="c-title">数据列表</div>
				<el-table :data="dataList" size="mini">
					<el-table-column prop="id" label="编号" width="70px" > </el-table-column>
					<el-table-column label="图片" width="300px">
						<template slot-scope="s">
							<img :src="s.row.img_src" class="td-img" title="点击预览" @click="sa.showImage(s.row.img_src)">
							<span style="color: #666; padding-left: 0.5em;"> 点击预览</span>
						</template>
					</el-table-column>
					<el-table-column label="标题">
						<template slot-scope="s">
							<el-input size="mini" v-model="s.row.title" @input="s.row.is_update=true"></el-input>
						</template>
					</el-table-column>
					<el-table-column label="排序">
						<template slot-scope="s">
							<el-input size="mini" v-model="s.row.sort" @input="s.row.is_update=true"></el-input>
						</template>
					</el-table-column>
					<el-table-column label="点击量" prop="click_count" width="100px"> </el-table-column>
					<el-table-column label="状态" >
						<template slot-scope="s">
							<el-switch v-model="s.row.status" :active-value="1" :inactive-value="2" @change="s.row.is_update=true"></el-switch>
							<span style="color: #999;" v-if="s.row.status==1">显示</span>
							<span style="color: #999;" v-else>隐藏</span>
						</template>
					</el-table-column>
					<el-table-column label="创建时间" width="150px">
						<template slot-scope="s">{{sa.forDate(s.row.create_time, 2)}}</template>
					</el-table-column>
					<el-table-column label="操作">
						<template slot-scope="s">
							<el-badge is-dot class="item" :is-dot="s.row.is_update" style="margin: 5px 0;">
								<el-button class="c-btn" type="primary" icon="el-icon-edit" @click="update(s.row)">修改</el-button>
							</el-badge>
							<el-badge is-dot class="item" :is-dot="false" style="margin: 5px 0;">
								<el-button class="c-btn" type="danger" icon="el-icon-delete" @click="del(s.row)">删除</el-button>
							</el-badge>
						</template>
					</el-table-column>
				</el-table>
				<!-- 分页 -->
				<div class="page-box">
					<el-pagination background
						layout="total, prev, pager, next, sizes, jumper" 
						:current-page.sync="p.pageNo" 
						:page-size.sync="p.pageSize" 
						:total="dataCount" 
						:page-sizes="[1, 10, 20, 30, 40, 50, 100]" 
						@current-change="f5()" 
						@size-change="f5()">
					</el-pagination>
				</div>
			</div>		
			<!-- 添加一个 -->
			<div class="c-panel c-panel-add">
				<h4 class="c-title">添加一个</h4>	
				<el-form v-if="m">
					<div class="c-item br">
						<label class="c-label">图片：</label>
						<img :src="m.img_src" class="td-img" @click="sa.showImage(m.img_src, '70%', '80%')" >
						<span class="c-remark"> 点击图片预览</span>
					</div>
					<div class="c-item br">
						<label class="c-label">标题：</label>
						<el-input size="mini" v-model="m.title"></el-input>
					</div>
					<div class="c-item br">
						<label class="c-label">排序：</label>
						<el-input size="mini" v-model="m.sort" type="number"></el-input>
					</div>
					<div class="c-item br">
						<label class="c-label"></label>
						<el-button size="mini" type="primary" size="mini" icon="el-icon-plus" @click="add" >添加</el-button>
					</div>
				</el-form>
			</div>
		</div>
		<!-- 模拟数据 -->
		<script src="mock-data-list.js"></script>
        <script>
			var app = new Vue({
				el: '.vue-box',
				data: {
					sa: sa, 	// 超级对象
					p: {		// 查询参数
						pageNo: 1,
						pageSize: 10,
						sort_type: 0
					},
					dataCount: 0,
					dataList: [],	// 数据集合 
					m: {		// 添加信息
						id: 11, 
						title: '标题',
						type: 1, 
						sort: 0,
						status:1, 
						img_src: 'http://color-test.oss-cn-qingdao.aliyuncs.com/dyc/img/2020/01/20/1579506071035455989950.jpeg',
						create_time: new Date(),
						click_count: 42,
						is_update: false,
					},
					curr_m: null // 当前操作的 m 
				},
				methods: {
					// 刷新
					f5: function(){
						sa.ajax2('/SysSwiper/getList', this.p, function(res){
							this.dataList = sa.listAU(res.data);	// 数据
							this.dataCount = res.dataCount;			// 分页 
						}.bind(this), {res: mockDataList});
					},
					// 保存
					add: function(){
						var m = sa.copyJSON(this.m);
		                sa.ajax2('/SysSwiper/add', m, function(res){
		                    sa.alert('添加成功', function(){
		                        this.dataList.push(m);
		                    }.bind(this));
		                }.bind(this))
					},
					// 修改
					update: function(data){
						var data2 = sa.copyJSON(data);
						data2.create_time = undefined;
						sa.ajax2('/SysSwiper/update', data2, function(res){
							sa.ok('修改成功');
							data.is_update = false;
						})
					},
					// 删除
					del: function(data){
						sa.confirm('是否删除，此操作不可撤销', function(){
							sa.ajax2('/SysSwiper/delete?id=' + data.id, function(res){
								sa.ok('删除成功');
								sa.arrayDelete(this.dataList, data);
							}.bind(this))
						}.bind(this))
					}
				},
				created: function(){
					this.f5();
				}
			})
			
		</script>
</html>